package com.hulk.dryad.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.hulk.dryad.admin.api.vo.SysDeptRelationVO;
import com.hulk.dryad.admin.api.vo.SysDeptVO;
import com.hulk.dryad.admin.mapstruct.SysDeptRelationStruct;
import com.hulk.dryad.admin.mapstruct.SysDeptStruct;
import com.hulk.dryad.common.annotation.Idempotent;
import com.hulk.dryad.common.annotation.OperLog;
import com.hulk.dryad.common.annotation.PrintlnLog;
import com.hulk.dryad.common.component.R;
import com.hulk.dryad.common.constant.enums.ActionType;
import com.hulk.dryad.common.constant.enums.BEC;
import com.hulk.dryad.common.constant.enums.DelFlag;
import com.hulk.dryad.persistence.entity.SysDeptModel;
import com.hulk.dryad.persistence.entity.SysDeptRelationModel;
import com.hulk.dryad.manage.provider.service.SysDeptRelationService;
import com.hulk.dryad.manage.provider.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 部门管理 前端控制器
 * </p>
 *
 * @author kim
 * @since 2018-01-20
 */
@ApiSort(7)
@Api(value = "dept", tags = "部门管理模块")
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/dept")
public class SysDeptController {

	private final SysDeptRelationService relationService;

	private final SysDeptService sysDeptService;

	/**
	 * 通过ID查询
	 * @param id ID
	 * @return SysDeptVO
	 */
	@ApiOperation(value = "getById", notes = "通过ID查询部门信息")
	@PrintlnLog
	@GetMapping("/{id}")
	public R getById(@PathVariable Integer id) {
		SysDeptModel  model =	sysDeptService.getById(id);
		SysDeptVO vo = SysDeptStruct.INSTANCE.from(model);
		return R.ok(vo);
	}

	/**
	 * 验证部门名称是否存在
	 * @param name  部门名
	 * @return count
	 */
	@ApiOperation(value = "checkByName", notes = "验证部门名称是否存在")
	@PrintlnLog
	@GetMapping("/name/{name}")
	public R checkByName(@PathVariable String name) {
		int count =sysDeptService.count(Wrappers.<SysDeptModel>lambdaQuery().eq(SysDeptModel::getName,name));
		if(count>0){
			return R.failed(BEC.E_1006,count);
		}
		return R.ok(count);
	}

	/**
	 * 返回树形菜单集合
	 * @return 树形菜单
	 */
	@ApiOperation(value = "getTree", notes = "返回树形菜单集合")
	@PrintlnLog
	@GetMapping(value = "/tree")
	public R getTree() {
		return R.ok(sysDeptService.selectTree());
	}

	/**
	 * 添加
	 * @param vo 实体
	 * @return boolean
	 */
	@ApiOperation(value = "saveDept", notes = "添加部门")
	@Idempotent
	@OperLog(value  ="添加部门",action = ActionType.ADD)
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_dept_add')")
	public R save(@Valid @RequestBody SysDeptVO vo) {
		SysDeptModel model = SysDeptStruct.INSTANCE.to(vo);
		return R.ok(sysDeptService.saveDept(model));
	}

	/**
	 * 删除
	 * @param id ID
	 * @return boolean
	 */
	@ApiOperation(value = "removeById", notes = "删除部门")
	@Idempotent
	@OperLog(value  ="删除部门",action = ActionType.DEL)
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_dept_del')")
	public R removeById(@PathVariable Integer id) {
		return R.ok(sysDeptService.removeDeptById(id));
	}

	/**
	 * 编辑
	 * @param vo 实体
	 * @return boolean
	 */
	@ApiOperation(value = "updateDept", notes = "编辑部门")
	@Idempotent
	@OperLog(value  ="编辑部门",action = ActionType.UPDATE)
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_dept_edit')")
	public R update(@Valid @RequestBody SysDeptVO vo) {
		SysDeptModel model = SysDeptStruct.INSTANCE.to(vo);
		model.setUpdatedTime(LocalDateTime.now());
		return R.ok(sysDeptService.updateDeptById(model));
	}

	/**
	 * 查收子级列表
	 * @param deptId 部门ID
	 * @return 返回子级
	 */
	@ApiOperation(value = "getDescendantList", notes = "查收子级列表")
	@PrintlnLog
	@GetMapping(value = "/getDescendantList/{deptId}")
	public R getDescendantList(@PathVariable Integer deptId) {
	    List<SysDeptRelationModel> models =	relationService.list(Wrappers.<SysDeptRelationModel>lambdaQuery().eq(SysDeptRelationModel::getAncestor, deptId));
		List<SysDeptRelationVO> vos =  SysDeptRelationStruct.INSTANCE.from(models);
		return R.ok(vos);
	}

}
